<?php
class View{
	private $viewFile;
	private $controllerFile;
	private $controllerName;
	private $action;
	private $mvc;
	private $context;
	private $tpl;
	private static $_ = array();
	
	/**
	 * contructor	 
	 * @param unknown_type $mvc
	 */
	function __construct($mvc,$context){
		$this->setMVC($mvc);
		$this->context = $context;
		$this->initialize();		
	}
	
	private function initialize(){
		$this->viewFile = $this->mvc["view"];
		$this->controllerFile = $this->mvc["controller"]["path"];
		$this->controllerName = $this->mvc["controller"]["name"];
		$this->action = $this->mvc["action"];
	}
	
	/**
	 * pass a value to a key from controller to use in view file	 
	 * @param mixed $key
	 * @param mixed $value
	 */
	public static  function assign($key,$value){
		self::$_[$key] = $value;		
	}
	
	/**
	 * @return a content of action after execute action and combine with view	 
	 */
	public function getContent(){
		try{
			if(is_null($this->controllerFile)){ //false url
				//TODO : create a error site
				echo "!Ooppsss . Nothing here :D";
			}else{			
				ob_start();	
				//include view self to use assign function and $_ variable
				include_once 'snc-core/View.php';				
				//include all modle file before execute action
				$this->includeModel();                                
                include_once 'AbstractController.php';
				//inclcude controller file to call action
				include_once $this->controllerFile; 
                               
				//create reflection object
				$rfl = new ReflectionClass($this->controllerName);
				//create instance for this controller
				$obj = $rfl->newInstance();
				//call action
				$obj->{$this->action}();
				//get tpl value
				$this->tpl = $obj->tpl;					
				//include content of view file (.phtml)
				//a defined variable from View can be read on this view as normal way	
				include_once 'snc-core/Paths.php';		
				include_once $this->viewFile;
				$content = ob_get_clean();									
				return $content;
			}
		}catch (Exception $e){
			return $e->getMessage();
		}
	}
	
	/**
	 * get all modelfiles and include them to use for controller
	 * Enter description here ...
	 */
	public function includeModel(){
		try{
			//open modul director
			$dir_handle = @opendir($this->getPathToModel());
	                
			$listModel = array();
			//read all model file
	               
			while(($file = @readdir($dir_handle)) != false){
	                        
				if($file != "." && $file != ".." && (strpos($file, ".php")>0)){
					$modelFile = $this->getPathToModel()."/".$file; 
					$listModel[] = $modelFile;
				}     
			}
	               
			//include abstractDb and AbstractMail before all model file
	//		include_once 'snc-core/AbstractDB.php';
	//		include_once 'snc-core/AbstractMail.php';
	//		AbstractDB::$context = $this->context;
	//		AbstractMail::$context = $this->context;
			include_once 'snc-core/AbstractModel.php';
			AbstractModel::$context = $this->context;
			//include /import all modelfiles to use for every controllerfiles
			foreach($listModel as $modelFiles){
				include_once $modelFiles;
			}
	        
			@closedir($dir_handle);
	        return $listModel;
		}catch(Exception $e){
			return $e->getMessage();
		}
               
	}
	
	/**
	 * get path to model director
	 */
	public function getPathToModel(){
		$pathArray = explode("/", $this->controllerFile);
                $lengthPath = count($pathArray);
		$cutPath = array_slice($pathArray, 0,$lengthPath-2);
		$cutPath[]= "model" ;
		return implode($cutPath, "/");
	}
	
	/**
	 * set avc (view, controller(path,name), action)	
	 * @param unknown_type $mvc
	 */
	public function setMVC($mvc){
		$this->mvc = $mvc;
	}
	
	/**
	 * get status of template that it's on or off
	 * after call action. This function can be only called after
	 * calling getContent otherwise it will return null
	 * @return tpl boolean. or null if getContent failed.	 
	 */
	public function isTplOn(){
		return $this->tpl;
	}

}